//
//  PhotoBrowserAnimator.m
//  PickerManager
//
//  Created by iOS开发 on 2018/8/15.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "PhotoBrowserAnimator.h"
#import "UIImageViewLocationModel.h"

@interface PhotoBrowserAnimator ()
/**判断当前动画是弹出还是消失*/
@property(nonatomic, assign, getter=isPresented) BOOL presented;

@end

@implementation PhotoBrowserAnimator

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    self.presented = YES;
    return self;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    self.presented = NO;
    return self;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.3f;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    if(self.isPresented){
        [self animationForPresentView:transitionContext];
    } else{
        [self animationForDismissView:transitionContext];
    }
}

//自定义弹出动画
- (void)animationForPresentView:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIView *presentView = [transitionContext viewForKey:UITransitionContextToViewKey];
    [transitionContext.containerView addSubview:presentView];
    
    UIView *backView = [[UIView alloc] init];
    backView.frame = presentView.frame;
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0;
    [transitionContext.containerView addSubview:backView];
    
    CGRect startRect = [self.photosModelArr[self.startIndex] imageViewFrame];
    UIImage *image = [self.photosModelArr[self.startIndex] cellImage];
    CGRect endRect = [self endRect:self.startIndex];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = [self.photosModelArr[self.startIndex] imageViewFrame];
    [transitionContext.containerView addSubview:imageView];
    imageView.frame = startRect;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    
    presentView.alpha = 0;
    transitionContext.containerView.backgroundColor = [UIColor blackColor];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        imageView.frame = endRect;
        backView.alpha = 1;
    }completion:^(BOOL finished) {
        presentView.alpha = 1.0;
        [backView removeFromSuperview];
        [imageView removeFromSuperview];
        transitionContext.containerView.backgroundColor = [UIColor clearColor];
        [transitionContext completeTransition:YES];
    }];
}

//自定义消失动画
- (void)animationForDismissView:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIView *dismissView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    [dismissView removeFromSuperview];
    
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
     UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVC.view];
    [containerView sendSubviewToBack:toVC.view];
    
    UIView *backView = [[UIView alloc] init];
    backView.frame = dismissView.frame;
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 1;
    [toVC.view addSubview:backView];
    
    UIImage *image = [self.photosModelArr[self.endIndex] cellImage];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = [self endRect:self.endIndex];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    [transitionContext.containerView addSubview:imageView];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        if (self.isDeletePop) {
            backView.transform = CGAffineTransformMakeTranslation(0, [UIScreen mainScreen].bounds.size.height);
            imageView.transform = CGAffineTransformMakeTranslation(0, [UIScreen mainScreen].bounds.size.height);
        } else {
            imageView.frame = [self.photosModelArr[self.endIndex] imageViewFrame];
            backView.alpha = 0;            
        }
    } completion:^(BOOL finished) {
        [backView removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
}


- (CGRect)endRect:(NSInteger)index {
    UIImage *image = [self.photosModelArr[index] cellImage];
    //计算imageView的frame
    CGFloat x = 0;
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = width / image.size.width * image.size.height;
    CGFloat y = 0;
    if(height < [UIScreen mainScreen].bounds.size.height){
        y = ([UIScreen mainScreen].bounds.size.height - height) * 0.5;
    }
    return CGRectMake(x, y, width, height);
}

- (void)changeEndIndex:(NSInteger)index {
    _endIndex = index;
}

@end
